package com.bibrApp.difu.fragment.home_mvp;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.bibrApp.difu.Response.BannerResBody;
import com.bibrApp.difu.Response.HomeMarketItemBean;
import com.bibrApp.difu.Response.LoginResponseBody;
import com.bibrApp.difu.Response.NoticeResBody;
import com.bibrApp.difu.api.RetrofitNet;
import com.bibrApp.difu.base.BaseObserver;
import com.bibrApp.difu.base.BasePresenter;
import com.bibrApp.difu.utils.DialogUtils;
import com.bibrApp.difu.utils.EncryptUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class HomePresenter extends BasePresenter<IHomeView> {

    public HomePresenter(Context context, IHomeView iView) {
        super(context, iView);
    }


    public void getNoticeData() {
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("typeId", "1");
        params.put("platform", "1");
        params.put("flag", "10");
        RetrofitNet.getInstance()
                .getApi()
                .getArticlesNoticeList(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(params)))
                .compose(getFragmentLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            String jsonStr = EncryptUtils.objectToJson(value);
                            NoticeResBody noticeResBody = new Gson().fromJson(jsonStr, NoticeResBody.class);
                            iView.successOfNotice(noticeResBody);
                        }

                    }
                });

    }

    public void getBanner() {
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("status", "1");
        params.put("platform", "1");
        RetrofitNet.getInstance()
                .getApi()
                .getBanner(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(params)))
                .compose(getFragmentLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            String jsonStr = EncryptUtils.objectToJson(value);
                            BannerResBody bannerResBody = new Gson().fromJson(jsonStr, BannerResBody.class);
                            iView.successOfBanner(bannerResBody);
                        }
                    }
                });

    }
    public void getRecommandSocket(Socket socket ,String type) {
        JSONObject object = new JSONObject();
        try {
            object.put("exchangeType", type);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //事件名+对应的参数
        socket.emit("homeQuotes", object);
        socket.on("homeQuotes", new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                if (args[0] == null) {
                    return;
                }
                if (args[0] instanceof JSONArray) {
                    JSONArray obj = (JSONArray) args[0];
                    if (obj == null || obj.length() < 1) {
                        return;
                    }
                    List<HomeMarketItemBean> homeBiBrItemBeanList = new Gson().fromJson(obj.toString(),new TypeToken<List<HomeMarketItemBean>>(){}.getType());
                    iView.successOfMarket(homeBiBrItemBeanList);

                } else if (args[0] instanceof JSONObject) {
                    int pos = 0;
                    JSONObject jsonObject = (JSONObject) args[0];

                }
            }
        });
    }
}
